package com.egreen.pointsmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egreen.common.States;
import com.egreen.common.pojo.DTO.pointsmall.UserLoginDTO;
import com.egreen.common.pojo.DTO.pointsmall.UserRegisterDTO;
import com.egreen.common.pojo.entity.pointsmall.PointsUser;
import com.egreen.pointsmall.dao.PointsUserDao;
import com.egreen.pointsmall.exception.PointsmallException;

import com.egreen.pointsmall.service.IPointsUserService;
import com.egreen.pointsmall.utils.MD5Util;
import com.egreen.pointsmall.utils.RegexValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author yyf
 * @since 2023-04-05
 */
@Service
@Slf4j
public class PointsUserServiceImpl extends ServiceImpl<PointsUserDao, PointsUser> implements IPointsUserService {
    @Autowired
    private PointsUserDao pointsUserDao;

    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    @Override
    public PointsUser register(UserRegisterDTO userRegisterDTO) {
        //用户名是否可用
        QueryWrapper<PointsUser> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("login_name", userRegisterDTO.getLoginName());
        PointsUser user = pointsUserDao.selectOne(userQueryWrapper);
        if (user!=null){
            throw new PointsmallException(States.USERNAME_EXISTS);
        }
        //邮箱格式的校验
        if (!RegexValidateUtil.checkEmail(userRegisterDTO.getEmail())){
            throw new PointsmallException(States.EMAIL_ERROR);
        }
        //手机格式的校验
        if (!RegexValidateUtil.checkMobile(userRegisterDTO.getMobile())){
            throw new PointsmallException(States.MOBILE_ERROR);
        }
        //存储数据库
        PointsUser pointsUser=new PointsUser();
        String saltMD5 = MD5Util.getSaltMD5(userRegisterDTO.getPassword());
        BeanUtils.copyProperties(userRegisterDTO,pointsUser);
        pointsUser.setPassword(saltMD5);
        int insert = pointsUserDao.insert(pointsUser);
        if (insert!=1){
            log.info("用户注册添加用户失败");
            throw new PointsmallException(States.USER_REGISTER_ERROR);
        }
        return pointsUser;
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @Override
    public PointsUser login(UserLoginDTO userLoginDTO) {
        //判断用户名是否存在
        QueryWrapper<PointsUser>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("login_name", userLoginDTO.getLoginName());
        PointsUser pointsUser = pointsUserDao.selectOne(queryWrapper);
        if (pointsUser==null){
            //表示输入的用户名不存在
            log.info("用户名不存在");
            throw new PointsmallException(States.USERNAME_NOT_EXISTS);
        }
        //表示用户名存在,校验密码
        //密码错误
        if(!(MD5Util.getSaltverifyMD5(userLoginDTO.getPassword(), pointsUser.getPassword()))){
            throw new PointsmallException(States.PASSWORD_ERROR);
        }
        return pointsUser;
    }

}
